/**
 * @param {WebMail} oWebMail
 * @return {WebMailMailScreen}
 */
function WebMailMailScreen (oWebMail)
{
	this.cache = {
		Hashes: {},
		Lists: {},
		Messages: {},
		Screens: {}
	};

	this.searchDesc = null;
	this.selectedFolder = null;
	this.currentListHash = null;
	this.selectedLines = {};

	this.webmail = oWebMail;
	this.screenObj = $('#mail-screen');
	this.currentPane = $('<div>');

	this.builder = new WebMailTemplateDataBuilder(this.webmail, this);
	this.pre = new WebMailScreenPrefetch(this.webmail, this);
	this.tabs = new WebMailTabs(this.webmail, this);

	return this;
}

WebMailMailScreen.prototype =
{
	initScreen: function ()
	{
		var obj = this;

		$.History.bind(function (sHash) {
			obj.webmail.prepearHash(sHash);
		});

		var iTimeOut = null;
		$('#info', this.screenObj).bind('ajaxSend', function (){
			var obj2 = this;
			iTimeOut = setTimeout(function() {$(obj2).text('Loading ...').show();}, 500);
		}).bind('ajaxComplete', function (){
			if (0 < iTimeOut) {clearTimeout(iTimeOut);}
			$(this).hide();
		});

		var oItem = this.webmail.parseHash($.History.getHash());
		var oParams = {
			folder: oItem.folder,
			search: (null === oItem.search) ? '' : oItem.search,
			page: (null === oItem.page) ? 1 : oItem.page,
			mpp: this.webmail.data.Settings.MessagePerPage
		};
		if (null !== oItem.uid)
		{
			oParams.uids = oItem.uid;
		}

		this.webmail.ajaxRequest('GetBase', oParams, false);

		$('#search-input', this.screenObj).keyup(function (event) {
			if ('13' == event.keyCode) {
				var sSearchDesc = $.trim($(this).val());
				obj.searchDesc = (0 < sSearchDesc.length) ? sSearchDesc : null;
				obj.initSearchRequest();
			}
		});

		$('#logout-link', this.screenObj).click(function () {
			$.cookie('xwm_auth', null);
			obj.webmail.goToHash('');
			location.reload();
		});

		this.initAutocomplete();
		this.initLiveActions();

		$(this.screenObj).removeClass('hide');
	},

	initLiveActions: function ()
	{
		var obj = this;

		// folders
		$('#left-pane', this.screenObj).delegate('#folders .folder:not(.noselect)[rel]', 'click', function () {
			var oRelHash = obj.webmail.parseHash($(this).attr('rel'));
			var oCurrentHash = obj.webmail.parseHash(obj.currentPane.attr('rel'));
			obj.clearXActionDelete();
			if (null === oCurrentHash.uid && oCurrentHash.folder === oRelHash.folder)
			{
				obj.webmail.ajaxRequest('GetListHash', {folder: oRelHash.folder}, false);
			}

			obj.webmail.prepearHash($(this).attr('rel'));
		});

		$('#left-pane #compose', this.screenObj).delegate('#compose-link', 'click', function () {
			obj.webmail.prepearHash(obj.webmail.generateHash({screen: 'compose'}));
		});

		// list
		$('#right-pane', this.screenObj).delegate('.message-list .message-line[rel] .message-click-line', 'click', function (){
			var oParent = $(this).parent();
			if (oParent.hasClass('is-unread'))
			{
				oParent.removeClass('is-unread');
				var oItem = obj.webmail.parseHash(oParent.attr('rel'));
				setTimeout(function (){
					obj.webmail.ajaxRequest('SetListFlags', {setaction: 'AddSeen', folder: oItem.folder, uids: oItem.uid});
				}, 1000);
			}
			obj.webmail.prepearHash(oParent.attr('rel'));
		});

		$('#right-pane', this.screenObj).delegate('.pages .page[rel]', 'click', function (){
			obj.webmail.prepearHash($(this).attr('rel'));
		});

		$('#right-pane', this.screenObj).delegate('.list-item .message-line[rel] input.message-checkbox:checkbox', 'click', function (){
			obj.setSelectOfLine(this, $(this).parent(), $(this).is(':checked'))
		});

		$('#right-pane', this.screenObj).delegate('.list-item .message-line[rel] .flag', 'click', function (){
			var oParent = $(this).parent();
			var oItem = obj.webmail.parseHash(oParent.attr('rel'));
			if (oParent.hasClass('is-flagged'))
			{
				oParent.removeClass('is-flagged');
				obj.webmail.ajaxRequest('SetListFlags', {setaction: 'RemoveFlag', folder: oItem.folder, uids: oItem.uid});
			}
			else
			{
				oParent.addClass('is-flagged');
				obj.webmail.ajaxRequest('SetListFlags', {setaction: 'AddFlag', folder: oItem.folder, uids: oItem.uid});
			}
		});

		$('#right-pane', this.screenObj).delegate('.list-item .buttons .button[name^=action]', 'click', function () {
			obj.actionsList($(this).attr('name'));
		});
		
//		$('#right-pane', this.screenObj).delegate('.list-item select[name=actionSelSelect]', 'change', function () {
//			obj.actionsSelectList($(this).val());
//			$(':first-child', $(this).parent()).attr('selected', true);
//		});
//		$('#right-pane', this.screenObj).delegate('.list-item select[name=actionActSelect]', 'change', function () {
//			obj.actionsList($(this).val());
//			$(':first-child', $(this).parent()).attr('selected', true);
//		});

		// message
		$('#right-pane', this.screenObj).delegate('.message-item .body-tabs .button[name^=action]', 'click', function (){
			obj.actionsMessage($(this).attr('name'));
		});

		$('#right-pane', this.screenObj).delegate('.message-item .button[name^=action]', 'click', function () {
			obj.actionsMessage($(this).attr('name'));
		});

		$('#right-pane', this.screenObj).delegate('.message-item .attachment[rel]', 'click', function (){
			document.location = $(this).attr('rel');
		});
	},

	initAutocomplete: function ()
	{
		var obj = this;
		$('#search-input', this.screenObj).autocomplete(this.webmail.getAutoCompleteArray(), {
			selectFirst: false, minChars: 1, matchContains: true, delay: 500,
			formatItem: function (data) {
				return obj.encodeHtml(data.toString());
			},
			formatResult: function (data) {
				return data;
			}
		});
	},

	initSearchRequest: function ()
	{
		if (null === this.selectedFolder)
		{
			this.selectedFolder = 'INBOX';
		}

		this.webmail.prepearHash(this.webmail.generateHash({
			folder: this.selectedFolder,
			search: (null !== this.searchDesc) ? this.searchDesc : null
		}));
	},

	setSelectOfLine: function (obj, oRel, bIsSelect, bChangeData)
	{
		bChangeData = ('undefined' === typeof(bChangeData)) ? true : bChangeData;
		if (bIsSelect)
		{
			if (bChangeData)
			{
				this.selectedLines[$(oRel).attr('rel')] = true;
			}
			$(obj).attr('checked', true);
			$(oRel).addClass('selected');
		}
		else
		{
			if (bChangeData)
			{
				delete this.selectedLines[$(oRel).attr('rel')];
			}
			$(obj).attr('checked', false);
			$(oRel).removeClass('selected');
		}
	},

	actionsMessage: function (sButtonName)
	{
		var obj = this;
		var sListRel = this.currentPane.attr('rel');
		var oItem =  this.webmail.parseHash(sListRel);

		var messageTabsChangerHandle = function (sItemClass)
		{
			$('.body-tabs .body-tabs-item', obj.currentPane).removeClass('selected');
			$('.body-view .body-view-item', obj.currentPane).addClass('hide');

			$('.body-tabs .body-tabs-item.' + sItemClass, obj.currentPane).addClass('selected');
			$('.body-view .body-view-item.' + sItemClass, obj.currentPane).removeClass('hide');
		};

		if ('actionBack' === sButtonName)
		{
			this.webmail.prepearHash((null === this.currentListHash)
				? this.webmail.generateHash({folder: oItem.folder}) : this.currentListHash);
		}
		else if ('actionSave' === sButtonName)
		{
			document.location = this.webmail.mailUrl + '?action=save&folder=' + encodeURIComponent(oItem.folder)
				+ '&uid=' + encodeURIComponent(oItem.uid);
		}
		else if ('actionHtml' === sButtonName)
		{
			messageTabsChangerHandle('html');
		}
		else if ('actionPlain' === sButtonName)
		{
			messageTabsChangerHandle('plain');
		}
		else if ('actionShowImages' === sButtonName)
		{
			$('.body-tabs input:button[name=actionShowImages]', this.currentPane).addClass('hide');
			$('.body-view-item.html img[data-x-src]', this.currentPane).each(function(){
				$(this).attr('src', $(this).attr('data-x-src')).removeAttr('data-x-src');
			});
			$('.body-view-item.html [data-x-background]', this.currentPane).each(function(){
				$(this).attr('background', $(this).attr('data-x-background')).removeAttr('data-x-background');
			});

			$('.body-view-item.html [data-x-background-image]', this.currentPane).each(function(){
				$(this).attr('style', $(this).attr('style').replace(/static\/img\/src.png/m, $(this).attr('data-x-background-image')))
					.removeAttr('data-x-background-image');
			});
		}
	},

	actionsSelectList: function (sActionName)
	{
		var obj = this;
		var oFullList = $('.message-list .message-line', this.currentPane);
		switch (sActionName)
		{
			case 'actionSelectAll':
				oFullList.each(function () {
					var oParent = this;
					obj.setSelectOfLine($('.message-checkbox:checkbox', this).first(), oParent, true);
				});
				break;
			case 'actionUnselectAll':
				oFullList.each(function () {
					var oParent = this;
					obj.setSelectOfLine($('.message-checkbox:checkbox', this).first(), oParent, false);
				});
				break;
			case 'actionSelectRead':
				oFullList.each(function () {
					var oParent = this;
					obj.setSelectOfLine($('.message-checkbox:checkbox', this).first(), oParent, !$(this).hasClass('is-unread'));
				});
				break;
			case 'actionSelectUnread':
				oFullList.each(function () {
					var oParent = this;
					obj.setSelectOfLine($('.message-checkbox:checkbox', this).first(), oParent, $(this).hasClass('is-unread'));
				});
				break;
			case 'actionSelectFlag':
				oFullList.each(function () {
					var oParent = this;
					obj.setSelectOfLine($('.message-checkbox:checkbox', this).first(), oParent, $(this).hasClass('is-flagged'));
				});
				break;
			case 'actionSelectUnflag':
				oFullList.each(function () {
					var oParent = this;
					obj.setSelectOfLine($('.message-checkbox:checkbox', this).first(), oParent, !$(this).hasClass('is-flagged'));
				});
				break;
		}
	},

	actionsList: function (sActionName)
	{
		var aItems = [];
		$('.message-list .message-line[rel] .message-checkbox:checked', this.currentPane).each(function () {
			aItems.push($(this).parent().attr('rel'));
		});

		var sListRel = this.currentPane.attr('rel');

		if (aItems && 0 < aItems.length)
		{
			var i,oList, oItem, sFolder, aUids = [];
			oList = this.webmail.parseHash(sListRel);
			sFolder = oList.folder;
			for (i in aItems)
			{
				oItem = this.webmail.parseHash(aItems[i]);
				aUids.push(oItem.uid);
			}

			var sUids = aUids.join(',');

			switch (sActionName)
			{
				case 'actionRead':
					for (i in aItems)
					{
						$('.message-list .message-line[rel=' + aItems[i] + ']', this.currentPane).removeClass('is-unread');
					}
					this.webmail.ajaxRequest('SetListFlags', {setaction: 'AddSeen', folder: sFolder, uids: sUids});
					break;
				case 'actionUnread':
					for (i in aItems)
					{
						$('.message-list .message-line[rel=' + aItems[i] + ']', this.currentPane).addClass('is-unread');
					}
					this.webmail.ajaxRequest('SetListFlags', {setaction: 'RemoveSeen', folder: sFolder, uids: sUids});
					break;
				case 'actionFlag':
					for (i in aItems)
					{
						$('.message-list .message-line[rel=' + aItems[i] + ']', this.currentPane).addClass('is-flagged');
					}
					this.webmail.ajaxRequest('SetListFlags', {setaction: 'AddFlag', folder: sFolder, uids: sUids});
					break;
				case 'actionUnflag':
					for (i in aItems)
					{
						$('.message-list .message-line[rel=' + aItems[i] + ']', this.currentPane).removeClass('is-flagged');
					}
					this.webmail.ajaxRequest('SetListFlags', {setaction: 'RemoveFlag', folder: sFolder, uids: sUids});
					break;
				case 'actionDelete':
				//	this.webmail.ajaxRequest('DeleteMessages', {folder: sFolder, uids: sUids});
					this.removeFromList(aItems, sListRel);
					break;
			}
		}

		return true;
	},

	removeFromList: function (aItems, sListRel)
	{
		var oList = this.webmail.parseHash(sListRel);
		var iCountToDelete = aItems.length;
		var currentPage = (null === oList.page) ? 1 : parseInt(oList.page);

		oList.page = currentPage + 1;
		var sCheckCacheKey = this.webmail.generateHash(oList);

		var aCacheRels = [];
		if ('undefined' !== typeof(this.cache.Lists[sCheckCacheKey]))
		{
			$('.message-line', this.cache.Lists[sCheckCacheKey]).each(function(){
				aCacheRels.push($(this));
			});
		}
		else
		{
			var aDomCacheRels = jQuery.data(this.currentPane, 'CacheRels');
			if (aDomCacheRels && 0 < aDomCacheRels.length)
			{
				aCacheRels = aDomCacheRels;
			}
		}

		var i = 0;
		if (iCountToDelete <= aCacheRels.length)
		{
			for (i in aItems)
			{
				delete this.selectedLines[aItems[i]];
				$('.message-line[rel=' + aItems[i] + ']', this.currentPane).remove();
			}

			i = 0;
			while (0 < aCacheRels.length && i < iCountToDelete)
			{
				var oAddLi = aCacheRels.shift();
				if ('undefined' !== typeof(this.selectedLines[oAddLi.attr('rel')]))
				{
					this.setSelectOfLine($('input:checkbox', oAddLi), oAddLi, true, false);
				}
				oAddLi.appendTo('.message-list', this.currentPane);
				i++;
			}

			jQuery.data(this.currentPane, 'CacheRels', aCacheRels);
			this.clearListCacheFromPage(oList.folder, currentPage + 1);
		}
		else
		{
			for (i in aItems)
			{
				delete this.selectedLines[aItems[i]];
			}

			this.clearListCacheFromPage(oList.folder, currentPage, true);
		}
	},

	/**
	 * @param {string} sAction
	 * @param {object} oItem
	 * @return {void}
	 */
	buildResponseItem: function (sAction, oItem)
	{
		var sListItemKey, oAddDiv, iKey;
		var bIsBase = ('undefined' !== typeof(oItem.Base) && oItem.Base);

		if ('GetList' === sAction)
		{
			var Search = ('undefined' !== typeof(oItem.Search) && null !== oItem.Search && 0 < oItem.Search.length) ? oItem.Search : null;
			sListItemKey = this.webmail.generateHash({
				folder: oItem.Folder, page: oItem.Page, search: Search
			});

			// this.pre.currentData(sAction, oItem);

			oAddDiv = this.builder.buildMessageList(oItem);
			if ('undefined' !== typeof(oItem.Autocomplete))
			{
				for (var sKey in oItem.Autocomplete)
				{
					this.webmail.autoCompleteData[sKey] = oItem.Autocomplete[sKey];
				}

				this.initAutocomplete();
			}

			sCheckFolderHash = this.webmail.generateHash({folder: oItem.Folder});
			if ('undefined' !== typeof(this.cache.Hashes[sCheckFolderHash])
				&& oItem.Hash !== this.cache.Hashes[sCheckFolderHash])
			{
				this.clearListCache(oItem.Folder);
			}

			this.cache.Hashes[sCheckFolderHash] = oItem.Hash;

			if (null !== oAddDiv) // add to list
			{
				this.cache.Lists[sListItemKey] = oAddDiv;
				oAddDiv.appendTo('#right-pane #center-pane #list-cache');

				if (bIsBase)
				{
					this.webmail.prepearHash(sListItemKey, true);
				}
				else
				{
					if (sListItemKey === this.currentPane.attr('rel') && 'delete' === this.currentPane.attr('data-x-action'))
					{
						this.webmail.prepearHash(sListItemKey, true);
					}
				}
			}
		}
		else if ('GetMessages' === sAction)
		{
			if ('undefined' !== typeof(oItem.Response.length))
			{
				sListItemKey = null;
				var oShow = this.builder.buildQuotedLink();
				for (iKey in oItem.Response)
				{
					if ('undefined' !== typeof(oItem.Response[iKey].Uid))
					{
						sListItemKey = this.webmail.generateHash({
							folder: oItem.Response[iKey].Folder, uid: oItem.Response[iKey].Uid
						});

						oAddDiv = this.builder.buildMessage(oItem.Response[iKey]);
						
						var mailtoHandle = function(event)
						{
							//TODO
							event.preventDefault();
							alert('TODO:\r\n' + $(this).attr('href'));
						}

						if (oItem.Response[iKey].Html)
						{
							var oHtmlItem = $('.body-view-item.html', oAddDiv);
							oHtmlItem.html(oItem.Response[iKey].Html);
							oHtmlItem.find('blockquote').each(function () {
								$(this).hide();
								var obj = this;
								var oClone = oShow.clone().click(function () {
									$(this).remove();
									$(obj).show();
								});
								oClone.insertBefore(this);
							});
							oHtmlItem.find('a[href^=mailto]').click(mailtoHandle);
						}

						if (oItem.Response[iKey].Plain)
						{
							$('.body-view-item.plain', oAddDiv).html(oItem.Response[iKey].Plain);
							$('.body-view-item.plain', oAddDiv).find('a[href^=mailto]').click(mailtoHandle);
						}

						this.cache.Messages[sListItemKey] = oAddDiv;
						this.cache.Messages[sListItemKey].appendTo('#right-pane #center-pane #message-cache');
					}
					else
					{
						if (this.currentPane)
						{
							this.clearListCache(oItem.Folder, true);
						}
						this.webmail.ajaxAppError('', 'Message is null');
						break;
					}
				}

				if (null !== sListItemKey && bIsBase)
				{
					this.webmail.prepearHash(sListItemKey, true);
				}
			}
		}
		else if ('GetListHash' === sAction)
		{
			var sCheckFolderHash = this.webmail.generateHash({folder: oItem.Folder});
			if ('undefined' !== typeof(this.cache.Hashes[sCheckFolderHash])
				&& oItem.Response !== this.cache.Hashes[sCheckFolderHash])
			{
				this.cache.Hashes[sCheckFolderHash] = oItem.Response;
				this.clearListCache(oItem.Folder, true);
			}
		}
		else if ('GetFolders' === sAction)
		{
			if (0 < oItem.Response.length)
			{
				this.webmail.data.Folders = oItem.Response;

				oAddDiv = this.builder.buildFolderList(this.webmail.data.Folders);
				$('#left-pane #folders', this.screenObj).empty().append(oAddDiv);

				if (null !== this.selectedFolder)
				{
					this.setActiveFolder(this.selectedFolder);
				}
			}
		}
	},

	/**
	 * @param {string} sHash
	 * @return {bool}
	 */
	buildHash: function (sHash)
	{
		var sListItemKey, oHash = this.webmail.parseHash(sHash);

		var sFolder = oHash.folder;
		var sUid = oHash.uid || null;
		var iPage = oHash.page || null;
		var sSearch = oHash.search || null;
		var sScreen = oHash.screen;
		var bDoRequest = false;

		if ('compose' === sScreen) // compose
		{
			sListItemKey = this.webmail.generateHash({screen: sScreen, folder: sFolder, uid: sUid});
			if ('undefined' === typeof(this.cache.Screens[sListItemKey]))
			{
				var oAddDiv = this.builder.buildCompose();
				this.cache.Screens[sListItemKey] = oAddDiv;
				oAddDiv.appendTo('#right-pane #center-pane #draft-cache');
			}

			return true;
		}
		else if (null !== sUid && null === iPage) // message
		{
			sListItemKey = this.webmail.generateHash({folder: sFolder, uid: sUid});
			if ('undefined' === typeof(this.cache.Messages[sListItemKey]))
			{
				if (this.webmail.currentAction !== sHash)
				{
					bDoRequest = true;
				}

				if (bDoRequest)
				{
					this.webmail.ajaxRequest('GetMessages', {
						folder: sFolder,
						uids: sUid
					});
				}

				return false;
			}
		}
		else if (null !== iPage || null !== sFolder) // list
		{
			iPage = (null === iPage) ? 1 : iPage;
			sListItemKey = this.webmail.generateHash({folder: sFolder, page: iPage, search: sSearch});
			if ('undefined' === typeof(this.cache.Lists[sListItemKey]))
			{
				if (null === this.webmail.currentAction)
				{
					bDoRequest = true;
				}
				else
				{
					var oCurrentHash = this.webmail.parseHash(this.webmail.currentAction);
					oCurrentHash.page = (null === oCurrentHash.page) ? 1 : oCurrentHash.page;
					if (oCurrentHash.folder != sFolder || oCurrentHash.page != iPage || oCurrentHash.search != sSearch)
					{
						bDoRequest = true;
					}
				}
				if (bDoRequest)
				{
					this.webmail.ajaxRequest('GetList', {
						folder: sFolder,
						page: iPage,
						search: (null === sSearch) ? '' : sSearch,
						mpp: this.webmail.data.Settings.MessagePerPage
					});
				}

				return false;
			}
		}

		return true;
	},

	/**
	 * @param {string} sHash
	 * @return {bool}
	 */
	setActiveHash: function (sHash)
	{
		var oHash = this.webmail.parseHash(sHash);
		var sFolder = oHash.folder || null;
		var sUid = oHash.uid || null;
		var iPage = oHash.page || null;
		var sSearch = oHash.search || null;
		var sScreen = oHash.screen || null;

		if (null !== sFolder)
		{
			this.setActiveFolder(sFolder);
		}

		if ('mail' !== sScreen)
		{
			return this.setActiveSubScreen(this.webmail.generateHash({screen: sScreen}));
		}

		if (null !== sUid && null === iPage) // show message
		{
			return this.setActiveMessage(this.webmail.generateHash({folder: sFolder, uid: sUid}));
		}
		else if (null !== iPage || null !== sFolder) // show list
		{
			iPage = (null === iPage) ? 1 : iPage;
			$('#search-input', this.screenObj).val((sSearch && 0 < sSearch.length) ? sSearch : '');
			this.searchDesc = (sSearch && 0 < sSearch.length) ? sSearch : null;

			return this.setActiveList(this.webmail.generateHash({folder: sFolder, page: iPage, search: sSearch}));
		}

		return false;
	},

	setActiveSubScreen: function (sListItemKey)
	{
		if ('undefined' !== typeof(this.cache.Screens[sListItemKey]))
		{
			this.currentPane.addClass('hide');
			this.currentPane = this.cache.Screens[sListItemKey];
			this.currentPane.removeClass('hide');
			this.tabs.setActivTab(sListItemKey);
			return true;
		}
		return false;
	},

	/**
	 * @param {string} sListItemKey
	 * @return {bool}
	 */
	setActiveMessage: function (sListItemKey)
	{
		if ('undefined' !== typeof(this.cache.Messages[sListItemKey]))
		{
			this.currentPane.addClass('hide');
			this.currentPane = this.cache.Messages[sListItemKey];
			this.currentPane.removeClass('hide');
			this.tabs.setActivTab(sListItemKey);
			return true;
		}
		return false;
	},

	/**
	 * @param {string} sListItemKey
	 * @return {bool}
	 */
	setActiveList: function (sListItemKey)
	{
		if ('undefined' !== typeof(this.cache.Lists[sListItemKey]))
		{
			var obj = this;
			this.currentPane.addClass('hide');

			$('.message-list .message-line[rel] input:checkbox', this.currentPane).each(function () {
				obj.setSelectOfLine(this, $(this).parent(), false, false);
			});

			this.currentPane = this.cache.Lists[sListItemKey];

			for (var sKey in this.selectedLines)
			{
				$('.message-list .message-line[rel=' + sKey + '] input:checkbox', this.currentPane).each(function () {
					obj.setSelectOfLine(this, $(this).parent(), true, false);
				});
			}

			this.currentPane.removeClass('hide');
			this.currentListHash = sListItemKey;
			this.tabs.setActivTab(sListItemKey);
			return true;
		}
		return false;
	},
	
	/**
	 * @param {string} sFolder
	 * @return {void}
	 */
	setActiveFolder: function (sFolder)
	{
		if (null !== sFolder)
		{
			$('#folders .folder[rel]', this.screenObj).removeClass('selected');
			$('#folders .folder[rel=' + this.webmail.generateHash({folder: sFolder}) + ']', this.screenObj).addClass('selected');
			this.selectedFolder = sFolder;
		}
	},

	/**
	 * @param {string} sFolder
	 * @param {bool} bPrepearHash
	 * @return {bool}
	 */
	clearListCache: function (sFolder, bPrepearHash)
	{
		bPrepearHash = ('undefined' === typeof(bPrepearHash)) ? false : bPrepearHash;
		this.clearListCacheFromPage(sFolder, 1, bPrepearHash);
	},

	/**
	 * @param {string} sFolder
	 * @param {int} iPage
	 * @param {bool} bPrepearHash
	 * @return {bool}
	 */
	clearListCacheFromPage: function (sFolder, iPage, bPrepearHash)
	{
		var oItem , sCacheName;
		iPage = ('undefined' === typeof(iPage)) ? 1 : iPage;
		bPrepearHash = ('undefined' === typeof(bPrepearHash)) ? false : bPrepearHash;
		for (sCacheName in this.cache.Lists)
		{
			oItem = this.webmail.parseHash(sCacheName);
			oItem.page = (null === oItem.page) ? 1 : oItem.page;
			if (oItem.folder === sFolder && oItem.page >= iPage)
			{
				this.cache.Lists[sCacheName].attr('data-x-action', 'delete');
				delete this.cache.Lists[sCacheName];
			}
		}

		if (bPrepearHash && this.currentPane)
		{
			if ('delete' === this.currentPane.attr('data-x-action'))
			{
				this.webmail.prepearHash(this.currentPane.attr('rel'));
			}
		}
	},

	/**
	 * @return {void}
	 */
	clearXActionDelete: function ()
	{
		$('#right-pane #center-pane div[rel].hide', this.screenObj).remove('[data-x-action=delete]');
	},

	/**
	 * @param {string} sText
	 * @return {string}
	 */
	encodeHtml: function (sText)
	{
		return sText.replace(/&/g, "&amp;").replace(/</g, "&lt;")
			.replace(/>/g, "&gt;").replace(/"/g, '&quot;').replace(/'/g, '&#039;');
	}
}